package com.swa99ylum.pjv.sample.s01_nodeframe.s0100;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.swa99ylum.pjv.Fuse;
import com.swa99ylum.pjv.PluggableJSONValidator;
import com.swa99ylum.pjv.bean.ArrayJSONNode;
import com.swa99ylum.pjv.bean.JSONNode;
import com.swa99ylum.pjv.bean.ObjectJSONNode;
import com.swa99ylum.pjv.sample.FileUtils;

public class Test1 {
	public static void main(String[] args) throws Exception {
		String filePath = Test1.class.getResource("json_0100.txt").getPath();
		
		//step1、获取 json
		String json = null;
		try {
			json = FileUtils.getFileContent(filePath);
		} catch (Exception e) {
			System.err.println("从 文件中，读取 json 有误");
		}
		
		System.out.println(json);
		
		//step2、定义 json 的 nodeFrame
		@SuppressWarnings("serial")
		Map<String, Object[]> nodeFrame = new HashMap<String, Object[]>(){{
			put("$[O]", new Object[]{
					new JSONNode("id", 				new FooValidator()),
					new JSONNode("c_date",			new FooValidator()),
					new JSONNode("name", 			new FooValidator()),
					new JSONNode("teacher", 		new FooValidator()),
					new JSONNode("grade", 			new FooValidator()),
					new ArrayJSONNode("student", 	new FooValidator()),
					new ObjectJSONNode("tv", 		new FooValidator())});
			
			put("$[O].tv[O]", new Object[]{
					new JSONNode("price", 			new FooValidator()), 
					new JSONNode("brand", 			new FooValidator()),
					new ObjectJSONNode("size1",		new FooValidator())});
			
			put("$[O].student[A]", new Object[]{
					new JSONNode("id", 			new FooValidator()), 
					new JSONNode("name", 		new FooValidator()), 
					new JSONNode("age", 		new FooValidator()), 
					new ArrayJSONNode("pet",	new FooValidator())});
			
			//由于 $.student 是 Array，所以 该节点 并不会被 匹配到
			put("$[O].student[O]", new Object[]{
					new JSONNode("id",			new FooValidator()), 
					new JSONNode("nickName",	new FooValidator())});
			
		}};
		
		//step3、定义 errorMap，用于存储 验证出来的 错误结果
		Map<String, String> errorMap = new LinkedHashMap<String, String>();
		
		//step4、验证 json
		new PluggableJSONValidator(json, nodeFrame, Fuse.OBJECT, errorMap).parse();
		
		for(Entry<String, String> entry : errorMap.entrySet()) {
			System.out.println(entry.getKey() + " => " + entry.getValue());
		}
		
		
	}
}
